package JDBC;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import Servidor.Clases.Sensor;
import Servidor.Clases.Vehiculo;

/**
 * 
 * Validado por Julen
 *
 */
public class JDBCsensor implements JDBC{
	
	private static final String DRIVER_CLASS_NAME = "org.sqlite.JDBC";
	private static final String CONNECTION_URL = "jdbc:sqlite:resources/BBDD/trenes.s3db";
	//private static final String CONNECTION_URL = "jdbc:sqlite:C:/Users/Julen/Desktop/trenes.s3db";
	private Connection connection;
	private ResultSet resulset;
	
	public JDBCsensor(){
		try {
			Class.forName(DRIVER_CLASS_NAME);
			connection= DriverManager.getConnection(CONNECTION_URL);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public void close() throws SQLException{
		connection.close();
	}
	
	@Override
	public int insert(Object o) throws SQLException, ClassNotFoundException {
		if(o instanceof Sensor){
			Statement statement = null;
			statement =  connection.createStatement();
			String sql="";
			Sensor v=(Sensor)o;
			String bool=v.isEncendido()?"1":"0";
			sql= "INSERT INTO sensor VALUES('"+v.getId()+"','"+v.getDesc()+"',"+bool+","+v.getTren().getIdTren()+");";
			int check=statement.executeUpdate(sql);
			statement.close();
			return check;
		}else
			throw new IllegalArgumentException("El parametro recibido debe ser de tipo Sensor!");
	}

	@Override
	public int delete(Object o) throws SQLException {
		if(o instanceof Sensor){
			Statement statement = null;
			statement =  connection.createStatement();
			String sql="";
			Sensor v=(Sensor)o;
			sql= "DELETE FROM sensor WHERE id=('"+v.getId()+"');";
			int check=statement.executeUpdate(sql);
			statement.close();
			return check;
		}else
			throw new IllegalArgumentException("El parametro recibido debe ser de tipo Sensor!");
	}
	
	public ArrayList<Sensor> selectAllTren(Vehiculo veh) throws SQLException{
		Statement statement = null;
		statement =  connection.createStatement();
		String sql="";
		sql= "Select * FROM sensor where idTren="+veh.getIdTren()+";";
		resulset=statement.executeQuery(sql);
		ArrayList<Sensor> lista= new ArrayList<Sensor>();
		while(resulset.next()){
			lista.add(convertSensor(resulset));
		}
		
		statement.close();
		return lista;
	}

	@Override
	public ArrayList<Sensor> selectAll() throws SQLException {
		Statement statement = null;
		statement =  connection.createStatement();
		String sql="";
		sql= "Select * FROM sensor;";
		resulset=statement.executeQuery(sql);
		ArrayList<Sensor> lista= new ArrayList<Sensor>();
		while(resulset.next()){
			lista.add(convertSensor(resulset));
		}
		
		statement.close();
		return lista;
	}

	@Override
	public Sensor selectOne(Object o) throws SQLException {
		if(o instanceof Integer){
			Integer inte=(Integer)o;
			Sensor sensor=null;
			Statement statement = null;
			statement =  connection.createStatement();
			String sql="";
			sql= "Select * FROM sensor WHERE id="+inte+";";
			resulset=statement.executeQuery(sql);
			if(resulset.next())
				sensor=convertSensor(resulset);
			statement.close();
			return sensor;
		}else
			throw new IllegalArgumentException("El parametro recibido debe ser de tipo Integer!");
	}

	@Override
	public int update(Object o) throws SQLException, ClassNotFoundException {
		int check=delete(o);
		if(check!=0){
			insert(o);
		}
		return check;
	}

	private Sensor convertSensor(ResultSet res) throws SQLException{
		Sensor sensor=new Sensor(0,null,false,null);
		sensor.setId(res.getInt("id"));
		sensor.setDesc(res.getString("Desc"));
		sensor.setTren(new JDBCvehiculos().selectOne(res.getInt("idTren")));
		sensor.setEncendido(res.getBoolean("encendido"));
		return sensor;
	}

}
